﻿package njClass.bat{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.events.DataEvent;
	import flash.filters.GlowFilter;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class TextPlus {
		//变成文本输入框（resultFun（txt:String)返回函数，接收文本输入值）
		public static function IntoInput(txt:TextField):void {
			txt.type=TextFieldType.INPUT;
			txt.selectable=true;
			txt.setSelection(0,txt.length);
			var tempTxt:String=txt.text;
			txt.stage.focus=txt;
			txt.stage.addEventListener(MouseEvent.MOUSE_DOWN,StopReName);
			txt.stage.addEventListener(KeyboardEvent.KEY_DOWN,StopReNameKey);
			
			function StopReName(e:MouseEvent):void {
				if (e.target!=txt) {
					txt.setSelection(0,0);
					txt.type = TextFieldType.DYNAMIC;
					txt.selectable=false;
					txt.stage.removeEventListener(MouseEvent.MOUSE_DOWN,StopReName);
					txt.stage.removeEventListener(KeyboardEvent.KEY_DOWN,StopReNameKey);
					txt.dispatchEvent(new DataEvent("rename",false,false,txt.text));
				}
			}
			
			function StopReNameKey(e:KeyboardEvent):void {
				if (e.charCode==13) {
					txt.setSelection(0,0);
					txt.type = TextFieldType.DYNAMIC;
					txt.selectable=false;
					txt.stage.removeEventListener(MouseEvent.MOUSE_DOWN,StopReName);
					txt.stage.removeEventListener(KeyboardEvent.KEY_DOWN,StopReNameKey);
					txt.dispatchEvent(new DataEvent("rename",false,false,txt.text));
				} else if (e.charCode==27) {
					txt.setSelection(0,0);
					txt.type = TextFieldType.DYNAMIC;
					txt.selectable=false;
					txt.text=tempTxt;
					txt.stage.removeEventListener(MouseEvent.MOUSE_DOWN,StopReName);
					txt.stage.removeEventListener(KeyboardEvent.KEY_DOWN,StopReNameKey);
					txt.dispatchEvent(new DataEvent("rename",false,false,txt.text));
				}
			}
		}
		
		//文本框可透明化设置
		public static function TextAlpha(txt:TextField):void {
			var filter:GlowFilter = new GlowFilter(txt.textColor,0,0,0,0,3);
			txt.filters = [filter];
		}
		
		//按照delay间隔更新txt:TextField内容，getString(args)为String的变化函数
		public static function RunTxt(txt:TextField,getString:Function,args:*=null,delay:int=1000):void {
			var temp:String=getString(args);
			txt.text=temp;
			var timer:Timer=new Timer(delay);
			timer.addEventListener(TimerEvent.TIMER,reCheckTime);
			timer.start();

			function reCheckTime(e:TimerEvent):void {
				var temp:String=getString(args);
				txt.text=temp;
			}
		}
	}
}